﻿/*
    This software is licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php

    Copyright (C) 2010 Daniel Deptford (redmercury@gmail.com)
 
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:

        The above copyright notice and this permission notice shall be included in
        all copies or substantial portions of the Software.

        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
        THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using COLLADASharp.Serialization;

namespace COLLADASharp.Core
{
    [DAEClass("camera")]
    public class Camera : IDDAENode
    {
        [DAEAttribute("name")]
        [DAENodeProperty("Name", typeof(string))]
        private static NodeProperty nameProperty;
        public string Name
        {
            get { return GetValue(nameProperty) as string; }
            set { SetValue(nameProperty, value); }
        }

        [DAEElement(typeof(Asset), 0)]
        [DAENodeProperty("Asset", typeof(Asset))]
        private static NodeProperty assetProperty;
        public Asset Asset
        {
            get { return GetValue(assetProperty) as Asset; }
            set { SetValue(assetProperty, value); }
        }

        [DAEElement(typeof(Optics), 1)]
        [DAENodeProperty("Optics", typeof(Optics))]
        private static NodeProperty opticsProperty;
        public Optics Optics
        {
            get { return GetValue(opticsProperty) as Optics; }
            set { SetValue(opticsProperty, value); }
        }

        [DAEElement(typeof(Imager), 2)]
        [DAENodeProperty("Imager", typeof(Imager))]
        private static NodeProperty imagerProperty;
        public Imager Imager
        {
            get { return GetValue(imagerProperty) as Imager; }
            set { SetValue(imagerProperty, value); }
        }

        [DAEElement(typeof(Extra), 3)]
        [DAENodeProperty("Extra", typeof(Extra))]
        private static NodeProperty extraProperty;
        public Extra Extra
        {
            get { return GetValue(extraProperty) as Extra; }
            set { SetValue(extraProperty, value); }
        }
    }

    [DAEClass("imager")]
    public class Imager : DAENode
    {
        [DAEElement(typeof(Extra), 0)]
        [DAENodeProperty("Extra", typeof(Extra))]
        private static NodeProperty extraProperty;
        public Extra Extra
        {
            get { return GetValue(extraProperty) as Extra; }
            set { SetValue(extraProperty, value); }
        }
    }

    [DAEClass("instance_camera")]
    public class InstanceCamera : SIDDAENode
    {
        [DAEAttribute("name")]
        [DAENodeProperty("Name", typeof(string))]
        private static NodeProperty nameProperty;
        public string Name
        {
            get { return GetValue(nameProperty) as string; }
            set { SetValue(nameProperty, value); }
        }

        [DAEAttribute("url")]
        [DAENodeProperty("Url", typeof(Url))]
        private static NodeProperty urlProperty;
        public string UrlProperty
        {
            get { return GetValue(urlProperty) as string; }
            set { SetValue(urlProperty, value); }
        }

        [DAEElement(typeof(Extra), 0)]
        [DAENodeProperty("Extra", typeof(Extra))]
        private static NodeProperty extraProperty;
        public Extra Extra
        {
            get { return GetValue(extraProperty) as Extra; }
            set { SetValue(extraProperty, value); }
        }
    }

    [DAEClass("library_cameras")]
    public class LibraryCameras : Library
    {
        [DAEElement(typeof(Asset), 0)]
        [DAENodeProperty("Asset", typeof(Asset))]
        private static NodeProperty assetProperty;
        public Asset Asset
        {
            get { return GetValue(assetProperty) as Asset; }
            set { SetValue(assetProperty, value); }
        }

        [DAEElement(typeof(Camera), 1)]
        [DAENodeProperty("Cameras", typeof(DAENodeList<Camera>))]
        private static NodeProperty camerasProperty;
        public DAENodeList<Camera> Cameras
        {
            get { return GetValue(camerasProperty) as DAENodeList<Camera>; }
            set { SetValue(camerasProperty, value); }
        }


        [DAEElement(typeof(Extra), 2)]
        [DAENodeProperty("Extra", typeof(Extra))]
        private static NodeProperty extraProperty;
        public Extra Extra
        {
            get { return GetValue(extraProperty) as Extra; }
            set { SetValue(extraProperty, value); }
        }
    }

    public abstract class Projection : DAENode
    {
        public class SIDFloat : SIDDAENode, Serialization.IDAESerializable
        {
            public SIDFloat(XmlReader reader, DAESerializer serializer)
            {
                reader.ReadStartElement();
                if (reader.MoveToAttribute("sid"))
                {
                    SID = reader.Value;
                }

                reader.MoveToElement();

                Value = float.Parse(reader.ReadInnerXml());

                reader.ReadEndElement();
            }

            public void Serialize(XmlWriter writer, DAESerializer serializer)
            {
                throw new NotImplementedException();
            }

            [DAENodeProperty("Value", typeof(float))]
            private static NodeProperty valueProperty;
            public float Value
            {
                get { object v = GetValue(valueProperty); if (v != null) { throw new NullReferenceException(); } return (float)v; }
                set { SetValue(valueProperty, value); }
            }
        }
    }

    [DAEClass("perspective")]
    public class Perspective : Projection
    {
        [DAEElement("xfov", typeof(SIDFloat), 0)]
        [DAENodeProperty("XFOV", typeof(SIDFloat))]
        private static NodeProperty xfovProperty;
        public SIDFloat XFOV
        {
            get { return GetValue(xfovProperty) as SIDFloat; }
            set { SetValue(xfovProperty, value); }
        }

        [DAEElement("yfov", typeof(SIDFloat), 1)]
        [DAENodeProperty("YFOV", typeof(SIDFloat))]
        private static NodeProperty yfovProperty;
        public SIDFloat YFOV
        {
            get { return GetValue(yfovProperty) as SIDFloat; }
            set { SetValue(yfovProperty, value); }
        }

        [DAEElement("aspect_ratio", typeof(SIDFloat), 2)]
        [DAENodeProperty("AspectRatio", typeof(SIDFloat))]
        private static NodeProperty aspectRatioProperty;
        public SIDFloat AspectRatio
        {
            get { return GetValue(aspectRatioProperty) as SIDFloat; }
            set { SetValue(aspectRatioProperty, value); }
        }

        [DAEElement("znear", typeof(SIDFloat), 3)]
        [DAENodeProperty("ZNear", typeof(SIDFloat))]
        private static NodeProperty zNearProperty;
        public SIDFloat ZNear
        {
            get { return GetValue(zNearProperty) as SIDFloat; }
            set { SetValue(zNearProperty, value); }
        }

        [DAEElement("zfar", typeof(SIDFloat), 4)]
        [DAENodeProperty("ZFar", typeof(SIDFloat))]
        private static NodeProperty zFarProperty;
        public SIDFloat ZFar
        {
            get { return GetValue(zFarProperty) as SIDFloat; }
            set { SetValue(zFarProperty, value); }
        }
    }

    [DAEClass("orthographic")]
    public class Orthographic : Projection
    {
        [DAEElement("xmag", typeof(SIDFloat), 0)]
        [DAENodeProperty("XMag", typeof(SIDFloat))]
        private static NodeProperty xMaxProperty;
        public SIDFloat XMag
        {
            get { return GetValue(xMaxProperty) as SIDFloat; }
            set { SetValue(xMaxProperty, value); }
        }

        [DAEElement("xymag", typeof(SIDFloat), 1)]
        [DAENodeProperty("YMag", typeof(SIDFloat))]
        private static NodeProperty yMagProperty;
        public SIDFloat YMag
        {
            get { return GetValue(yMagProperty) as SIDFloat; }
            set { SetValue(yMagProperty, value); }
        }

        [DAEElement("aspect_ratio", typeof(SIDFloat), 2)]
        [DAENodeProperty("AspectRatio", typeof(SIDFloat))]
        private static NodeProperty aspectRatioProperty;
        public SIDFloat AspectRatio
        {
            get { return GetValue(aspectRatioProperty) as SIDFloat; }
            set { SetValue(aspectRatioProperty, value); }
        }

        [DAEElement("znear", typeof(SIDFloat), 3)]
        [DAENodeProperty("ZNear", typeof(SIDFloat))]
        private static NodeProperty zNearProperty;
        public SIDFloat ZNear
        {
            get { return GetValue(zNearProperty) as SIDFloat; }
            set { SetValue(zNearProperty, value); }
        }

        [DAEElement("zfar", typeof(SIDFloat), 4)]
        [DAENodeProperty("ZFar", typeof(SIDFloat))]
        private static NodeProperty zFarProperty;
        public SIDFloat ZFar
        {
            get { return GetValue(zFarProperty) as SIDFloat; }
            set { SetValue(zFarProperty, value); }
        }
    }

    [DAEClass("optics")]
    public class Optics : DAENode
    {
        [DAEClass("technique_common")]
        public class TechniqueCommon : TechniqueBase
        {
            [DAEElement(typeof(Perspective), 0)]
            [DAEElement(typeof(Orthographic), 0)]
            private static NodeProperty projectionProperty = NodeProperty.Register("Projection", typeof(Projection), typeof(TechniqueCommon));
            public Projection Projection
            {
                get { return GetValue(projectionProperty) as Projection; }
                set { SetValue(projectionProperty, value); }
            }
        }

        [DAEElement(typeof(TechniqueCommon), -1)]
        [DAENodeProperty("Common", typeof(TechniqueCommon))]
        private static NodeProperty commonProperty;
        public TechniqueCommon Common
        {
            get { return GetValue(commonProperty) as TechniqueCommon; }
            set { SetValue(commonProperty, value); }
        }

        [DAEElement(typeof(Extra), 1)]
        [DAENodeProperty("Extra", typeof(Extra))]
        private static NodeProperty extraProperty;
        public Extra Extra
        {
            get { return GetValue(extraProperty) as Extra; }
            set { SetValue(extraProperty, value); }
        }
    }
}